package com.css.dzj.dzjap.common.audit.service.impl;



import com.css.dzj.dzjap.common.audit.service.SysAuditAsyncService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.audit.viewobjects.AuditFailVO;
import com.css.dzj.dzjap.common.audit.viewobjects.AuditSuccessVO;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.IPUtils;
import com.css.dzj.dzjap.common.util.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;


/**
 * 日志
 * @author zhouchanghe
 * @version 1.0.1
 * @date 2020.05.15
 */

@Service
public class SysAuditServiceImpl implements SysAuditService {



    @Autowired
    private SysAuditAsyncService sysAuditAsyncService;


    @Override
    public void saveSuccessAudit(String description, String auditModule, String auditLevel, String auditType,String auditData, HttpServletRequest request){
        AuditSuccessVO sysAudit = new AuditSuccessVO();
       //系统code
        sysAudit.setAppCode(Constant.AppCode);
        //操作实体类的json
        sysAudit.setAuditData(auditData);
        //用户ID
        sysAudit.setUserId(ServiceUtil.getUserId(request));
        //操作URL
        sysAudit.setAuditUrl(request.getRequestURI());
         //审计级别 normal /serious
        sysAudit.setAuditLevel(auditLevel);
        //ip
        sysAudit.setClientIp(IPUtils.getClientIp(request));
        //操作描述
        sysAudit.setDescription(description);
        //字典模块
        sysAudit.setAuditModule(auditModule);
        //字典操作行为
        sysAudit.setAuditType(auditType);
        sysAuditAsyncService.saveSuccessAudit(sysAudit);
    }

    @Override
    public void saveFailAudit(String description, String auditModule, String auditLevel, String auditType, String auditData, Throwable e, HttpServletRequest request){
        AuditFailVO sysAudit = new AuditFailVO();
        sysAudit.setAppCode(Constant.AppCode);
        //操作实体类的json
        sysAudit.setAuditData(auditData);
        //用户ID
        sysAudit.setUserId(ServiceUtil.getUserId(request));
        //操作URL
        sysAudit.setAuditUrl(request.getRequestURI());
        //审计级别 normal /serious
        sysAudit.setAuditLevel(auditLevel);
        //ip
        sysAudit.setClientIp(IPUtils.getClientIp(request));
        //操作描述
        sysAudit.setDescription(description);
        //字典模块
        sysAudit.setAuditModule(auditModule);
        //字典操作行为
        sysAudit.setAuditType(auditType);
        //异常捕捉，抛出异常的e
        sysAudit.setExtendData(stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace()));
        sysAuditAsyncService.saveFailAudit(sysAudit);
    }

    /**
     * 异常打印处理
     * @param exceptionName
     * @param exceptionMessage
     * @param elements
     * @return
     */
    private String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
          StringBuffer strbuff = new StringBuffer();
          for (StackTraceElement stet : elements) {
           strbuff.append(stet + "\n");
          }
        String message = exceptionName + ":" + exceptionMessage + "\n\t" + strbuff.toString();
         return message;
    }
}

